package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.EquipOperator;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.dao.entity.EquipOperatorDao;
import cc.comac.mom.entity.EquipOperatorEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;

@Component
public class EquipOperatorDataHandler extends ServiceImpl<EquipOperatorDao, EquipOperatorEntity>{

	@Autowired
	private EquipOperatorDao equipOperatorDao;
	
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	
	public ResultBean save(EquipOperator obj) {
		ResultBean resultBean = new ResultBean(true);
		// TODO Auto-generated method stub
		if (obj == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("数据为空");
			return resultBean;
		}
		EquipOperatorEntity innerData = obj.getInnerData();
		if (innerData == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("数据为空");
			return resultBean;
		}
		baseEntityDataUtil.setBaseEntityData(innerData);
		int insert = equipOperatorDao.insert(innerData);
		if (insert < 1) {
			resultBean.setSuccess(false);
			resultBean.setMessage("新增失败");
			return resultBean;
		}
		return resultBean;
	}
	
	public ResultBean saveBatch(List<EquipOperator> list) {
		ResultBean resultBean = new ResultBean(true);
		// TODO Auto-generated method stub
		if(list == null || list.size()<1) {
			resultBean.setSuccess(false);
			resultBean.setMessage("数据为空");
			return resultBean;
		}
		ArrayList<EquipOperatorEntity> listEntity = new ArrayList<EquipOperatorEntity>();
		for(EquipOperator item:list) {
			EquipOperatorEntity innerData = item.getInnerData();
			if(innerData == null) {
				resultBean.setSuccess(false);
				resultBean.setMessage("数据为空");
				return resultBean;
			}
			baseEntityDataUtil.setBaseEntityData(innerData);
			listEntity.add(innerData);
		}
		boolean saveBatch = saveBatch(listEntity);
		if(!saveBatch) {
			resultBean.setSuccess(false);
			resultBean.setMessage("新增失败");
			return resultBean;
		}
		return resultBean;
	}
	
	public ResultBean deleteBatch(List<EquipOperator> list) {
		ResultBean resultBean = new ResultBean(true);
		if(list == null || list.size()<1) {
			resultBean.setSuccess(false);
			resultBean.setMessage("数据为空");
			return resultBean;
		}
		ArrayList<Long> listIds = new ArrayList<Long>();
		
		for(EquipOperator item:list) {
			EquipOperatorEntity innerData = item.getInnerData();
			if(innerData == null) {
				resultBean.setSuccess(false);
				resultBean.setMessage("数据为空");
				return resultBean;	
			}
			listIds.add(innerData.getId());
		}
		List<EquipOperatorEntity> selectBatchIdsList = equipOperatorDao.selectBatchIds(listIds);
		if(selectBatchIdsList == null || selectBatchIdsList.size()<1) {
			resultBean.setSuccess(false);
			resultBean.setMessage("数据为空");
			return resultBean;	
		}
		for(EquipOperatorEntity item :selectBatchIdsList) {
			baseEntityDataUtil.deleteBaseEntityData(item);
		}
		boolean updateBatchById = updateBatchById(selectBatchIdsList);
		
		if(!updateBatchById) {
			resultBean.setSuccess(false);
			resultBean.setMessage("删除失败");
			return resultBean;
		}

		return resultBean;
	}
	
	public ResultDev selectByCondition(EquipOperator obj) {
		QueryWrapper<EquipOperatorEntity> queryWrapper = new QueryWrapper<EquipOperatorEntity>();
		queryWrapper.eq("row_status", 1);
		if (obj != null && obj.getInnerData() != null) {
			if (obj.getInnerData().getCreator() != null && !"".equals(obj.getInnerData().getCreator())) {
				queryWrapper.like("creator", obj.getInnerData().getCreator());
			}
			if (obj.getInnerData().getOperation()!= null && !"".equals(obj.getInnerData().getOperation())) {
				queryWrapper.like("operation", obj.getInnerData().getOperation());
			}
			if (obj.getInnerData().getContent()!= null && !"".equals(obj.getInnerData().getContent())) {
				queryWrapper.like("content", obj.getInnerData().getContent());
			}
			if (obj.getOperatorTimeBegin()!= null && obj.getOperatorTimeEnd()!= null) {
				queryWrapper.between("creation_time", obj.getOperatorTimeBegin(), obj.getOperatorTimeEnd());
			}
		}
		if (obj.getPageSize() == 0) {
			obj.setPageSize(10);
		}
		Page<EquipOperatorEntity> page = new Page<>(obj.getPageNum(), obj.getPageSize(), true);
		IPage<EquipOperatorEntity> iPage = equipOperatorDao.selectPage(page, queryWrapper);
		List<EquipOperatorEntity> list = iPage.getRecords();
		ResultDev resultDev = new ResultDev();
		resultDev.setEntityList(list);
		resultDev.setTotalQuantity((int) iPage.getTotal());
		resultDev.setPageCount((int) iPage.getPages());
		return resultDev;

	}
}
